/**
 * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sinbader.xiaole.admin.controller;

import cn.stylefeng.guns.base.auth.annotion.Permission;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.consts.ConstantsContext;
import cn.stylefeng.guns.base.log.BussinessLog;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.constant.Const;
import cn.stylefeng.guns.sys.core.constant.dictmap.UserDict;
import cn.stylefeng.guns.sys.core.constant.state.ManagerStatus;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.core.log.LogObjectHolder;
import cn.stylefeng.guns.sys.core.util.SaltUtil;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.model.UserDto;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.guns.sys.modular.system.warpper.UserWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.datascope.DataScope;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import cn.stylefeng.roses.kernel.model.response.SuccessResponseData;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.io.File;
import java.util.Map;
import java.util.UUID;

/**
 * 系统管理员控制器
 *
 * @author fengshuonan
 * @Date 2017年1月11日 下午1:08:17
 */
@Controller
@RequestMapping("/user/common_user")
@Validated
public class CommonUserController extends BaseController {

	private static String PREFIX = "/user/common_user/";

	@Autowired
	private UserService userService;

	/**
	 * 跳转到查看管理员列表的页面
	 *
	 * @author fengshuonan
	 * @Date 2018/12/24 22:43
	 */
	@RequestMapping("")
	public String index() {
		return PREFIX + "common_user.html";
	}

	/**
	 * 跳转到查看管理员列表的页面
	 *
	 * @author fengshuonan
	 * @Date 2018/12/24 22:43
	 */
	@RequestMapping("/common_user_add")
	public String addView() {
		return PREFIX + "common_user_add.html";
	}

	/**
	 * 跳转到角色分配页面
	 *
	 * @author fengshuonan
	 * @Date 2018/12/24 22:43
	 */
	@Permission
	@RequestMapping("/supplier_role_assign")
	public String roleAssign(@RequestParam Long userId, Model model) {
		model.addAttribute("userId", userId);
		return PREFIX + "common_user_roleassign.html";
	}

	/**
	 * 跳转到编辑管理员页面
	 *
	 * @author fengshuonan
	 * @Date 2018/12/24 22:43
	 */
	@Permission
	@RequestMapping("/common_user_edit")
	public String userEdit(@RequestParam Long userId) {
		User user = this.userService.getById(userId);
		LogObjectHolder.me().set(user);
		return PREFIX + "common_user_edit.html";
	}

	/**
	 * 获取用户详情
	 *
	 * @author fengshuonan
	 * @Date 2018/12/24 22:43
	 */
	@RequestMapping("/getUserInfo")
	@ResponseBody
	public SuccessResponseData getUserInfo(@RequestParam Long userId) {
		if (ToolUtil.isEmpty(userId)) {
			throw new RequestEmptyException();
		}

		this.userService.assertAuth(userId);
		return new SuccessResponseData(userService.getUserInfo(userId));
	}


	/**
	 * 修改当前用户的密码
	 *
	 * @author fengshuonan
	 * @Date 2018/12/24 22:43
	 */
	@RequestMapping("/changePwd")
	@ResponseBody
	public Object changePwd(@RequestParam("oldPassword") @NotBlank String oldPassword,
							@RequestParam("newPassword") @Length(min = 6, max = 12) String newPassword) {
		this.userService.changePwd(oldPassword, newPassword);
		return SUCCESS_TIP;
	}

	/**
	 * 查询管理员列表
	 *
	 * @author fengshuonan
	 * @Date 2018/12/24 22:43
	 */
	@RequestMapping("/list")
	@Permission
	@ResponseBody
	public Object list(@RequestParam(required = false) String name,
					   @RequestParam(required = false) String timeLimit,
					   @RequestParam(required = false) Long deptId) {

		//拼接查询条件
		String beginTime = "";
		String endTime = "";

		if (ToolUtil.isNotEmpty(timeLimit)) {
			String[] split = timeLimit.split(" - ");
			beginTime = split[0];
			endTime = split[1];
		}

		Page<Map<String, Object>> users = userService.selectGeneralUsers(null, name, beginTime, endTime, deptId);
		Page wrapped = new UserWrapper(users).wrap();
		return LayuiPageFactory.createPageInfo(wrapped);
//		if (LoginContextHolder.getContext().isAdmin()) {
//			Page<Map<String, Object>> users = userService.selectUsers(null, name, beginTime, endTime, deptId);
//			Page wrapped = new UserWrapper(users).wrap();
//			return LayuiPageFactory.createPageInfo(wrapped);
//		} else {
//			DataScope dataScope = new DataScope(LoginContextHolder.getContext().getDeptDataScope());
//			Page<Map<String, Object>> users = userService.selectUsers(dataScope, name, beginTime, endTime, deptId);
//			Page wrapped = new UserWrapper(users).wrap();
//			return LayuiPageFactory.createPageInfo(wrapped);
//		}
	}

	/**
	 * 添加管理员
	 *
	 * @author fengshuonan
	 * @Date 2018/12/24 22:44
	 */
	@RequestMapping("/add")
	@BussinessLog(value = "添加管理员", key = "account", dict = UserDict.class)
	@Permission(Const.ADMIN_NAME)
	@ResponseBody
	public ResponseData add(@Valid UserDto user) {
		this.userService.addUser(user);
		return SUCCESS_TIP;
	}

	/**
	 * 修改管理员
	 *
	 * @author fengshuonan
	 * @Date 2018/12/24 22:44
	 */
	@RequestMapping("/edit")
	@BussinessLog(value = "修改管理员", key = "account", dict = UserDict.class)
	@ResponseBody
	public ResponseData edit(UserDto user) {
		this.userService.editUser(user);
		return SUCCESS_TIP;
	}

	/**
	 * 删除管理员（逻辑删除）
	 *
	 * @author fengshuonan
	 * @Date 2018/12/24 22:44
	 */
	@RequestMapping("/delete")
	@BussinessLog(value = "删除管理员", key = "userId", dict = UserDict.class)
	@Permission
	@ResponseBody
	public ResponseData delete(@RequestParam Long userId) {
		if (ToolUtil.isEmpty(userId)) {
			throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
		}
		this.userService.deleteUser(userId);
		return SUCCESS_TIP;
	}

	/**
	 * 查看管理员详情
	 *
	 * @author fengshuonan
	 * @Date 2018/12/24 22:44
	 */
	@RequestMapping("/view/{userId}")
	@ResponseBody
	public User view(@PathVariable Long userId) {
		if (ToolUtil.isEmpty(userId)) {
			throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
		}
		this.userService.assertAuth(userId);
		return this.userService.getById(userId);
	}

	/**
	 * 重置管理员的密码
	 *
	 * @author fengshuonan
	 * @Date 2018/12/24 22:44
	 */
	@RequestMapping("/reset")
	@BussinessLog(value = "重置管理员密码", key = "userId", dict = UserDict.class)
	@Permission(Const.ADMIN_NAME)
	@ResponseBody
	public ResponseData reset(@RequestParam Long userId) {
		if (ToolUtil.isEmpty(userId)) {
			throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
		}
		this.userService.assertAuth(userId);
		User user = this.userService.getById(userId);
		user.setSalt(SaltUtil.getRandomSalt());
		user.setPassword(SaltUtil.md5Encrypt(ConstantsContext.getDefaultPassword(), user.getSalt()));
		this.userService.updateById(user);
		return SUCCESS_TIP;
	}

	/**
	 * 冻结用户
	 *
	 * @author fengshuonan
	 * @Date 2018/12/24 22:44
	 */
	@RequestMapping("/freeze")
	@BussinessLog(value = "冻结用户", key = "userId", dict = UserDict.class)
	@Permission(Const.ADMIN_NAME)
	@ResponseBody
	public ResponseData freeze(@RequestParam Long userId) {
		if (ToolUtil.isEmpty(userId)) {
			throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
		}
		//不能冻结超级管理员
		if (userId.equals(Const.ADMIN_ID)) {
			throw new ServiceException(BizExceptionEnum.CANT_FREEZE_ADMIN);
		}
		this.userService.assertAuth(userId);
		this.userService.setStatus(userId, ManagerStatus.FREEZED.getCode());
		return SUCCESS_TIP;
	}

	/**
	 * 解除冻结用户
	 *
	 * @author fengshuonan
	 * @Date 2018/12/24 22:44
	 */
	@RequestMapping("/unfreeze")
	@BussinessLog(value = "解除冻结用户", key = "userId", dict = UserDict.class)
	@Permission(Const.ADMIN_NAME)
	@ResponseBody
	public ResponseData unfreeze(@RequestParam Long userId) {
		if (ToolUtil.isEmpty(userId)) {
			throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
		}
		this.userService.assertAuth(userId);
		this.userService.setStatus(userId, ManagerStatus.OK.getCode());
		return SUCCESS_TIP;
	}

	/**
	 * 分配角色
	 *
	 * @author fengshuonan
	 * @Date 2018/12/24 22:44
	 */
	@RequestMapping("/setRole")
	@BussinessLog(value = "分配角色", key = "userId,roleIds", dict = UserDict.class)
	@Permission(Const.ADMIN_NAME)
	@ResponseBody
	public ResponseData setRole(@RequestParam("userId") Long userId,
								@RequestParam("roleIds") @NotBlank String roleIds) {
		//不能修改超级管理员
		if (userId.equals(Const.ADMIN_ID)) {
			throw new ServiceException(BizExceptionEnum.CANT_CHANGE_ADMIN);
		}
		this.userService.assertAuth(userId);
		this.userService.setRoles(userId, roleIds);
		return SUCCESS_TIP;
	}

	/**
	 * 上传图片
	 *
	 * @author fengshuonan
	 * @Date 2018/12/24 22:44
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/upload")
	@ResponseBody
	public String upload(@RequestPart("file") MultipartFile picture) {

		String pictureName = UUID.randomUUID().toString() + "." + ToolUtil.getFileSuffix(picture.getOriginalFilename());
		try {
			String fileSavePath = ConstantsContext.getFileUploadPath();
			picture.transferTo(new File(fileSavePath + pictureName));
		} catch (Exception e) {
			throw new ServiceException(BizExceptionEnum.UPLOAD_ERROR);
		}
		return pictureName;
	}

	/**
	 * 选择办理人
	 *
	 * @author fengshuonan
	 * @Date 2019-8-22 15:48
	 */
	@RequestMapping("/listUserAndRoleExpectAdmin")
	@ResponseBody
	public LayuiPageInfo listUserAndRoleExpectAdmin() {
		Page pageContext = LayuiPageFactory.defaultPage();
		IPage page = userService.listUserAndRoleExpectAdmin(pageContext);
		return LayuiPageFactory.createPageInfo(page);
	}
}
